﻿
#include <stdio.h>
#include "StdAfx.h"
#include <stdlib.h>
#include <conio.h>

#include <windows.h>
#include "BaseMap.h"
#include "MoveSnake.h"

#define		EAST	1
#define		WEST	2
#define		SOUTH	3
#define		NORTH	4

struct _Snake *g_pSnakeH;
struct _Snake *g_pSnakeT;
struct _Snake *g_pSnakeT1;
int g_nHeadX;
int g_nHeadY;
int g_nTailX;
int g_nTailY;
int g_nHeadDir = EAST;
int state=1;

void gotoxy(int nX, int nY)
{
	COORD Pos = {nX, nY};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Pos);
}

int HeadMove()
{
	
	int nKey;	
	struct _Snake *Snake;

	g_nHeadX= g_pSnakeH->nX;
	g_nHeadY= g_pSnakeH->nY;



	switch(g_nHeadDir)
	{
	case EAST:
		g_nHeadX++;
		break;
	case WEST: 
		g_nHeadX--;
		break;
	case SOUTH:
		g_nHeadY--;
		break;
	case NORTH:
		g_nHeadY++;
		break;
	}

	if(kbhit())
	{	
		nKey= getch();

		switch(nKey)
		{
		case 80 : //UP
			if(g_nHeadDir == EAST)
			{
				g_nHeadX--;
				g_nHeadY++;
			}
			else if(g_nHeadDir == WEST)
			{
				g_nHeadX++;
				g_nHeadY++;
			}
			else if(g_nHeadDir == SOUTH)
			{
				state =0;
			}

			g_nHeadDir=NORTH;
			break;

		case 72: // DOWN
			if(g_nHeadDir == EAST)
			{
				g_nHeadX--;
				g_nHeadY--;
			}
			else if(g_nHeadDir == WEST)
			{
				g_nHeadX++;
				g_nHeadY--;
			}
			else if(g_nHeadDir == NORTH)
			{
				state =0;
	
			}
			g_nHeadDir=SOUTH;
			break;

		case  77: //RIGHT

			if(g_nHeadDir == SOUTH)
			{
				g_nHeadX++;
				g_nHeadY++;
			}
			else if(g_nHeadDir == NORTH)
			{
				g_nHeadX++;
				g_nHeadY--;
			}
			else if(g_nHeadDir == WEST)
			{
				state =0;
			}
			g_nHeadDir=EAST;
			break;

		case 75:// LEFT 

			if(g_nHeadDir == SOUTH)
			{
				g_nHeadX--;
				g_nHeadY++;
			}
			else if(g_nHeadDir == NORTH)
			{
				g_nHeadX--;
				g_nHeadY--;
			}
			else if(g_nHeadDir == EAST)
			{
				state=0;
			}
			g_nHeadDir=WEST;
			break;

		}
	}

	gotoxy((g_pSnakeT->nX)+1, (g_pSnakeT->nY)+1);
	printf(" ");
	gotoxy((g_pSnakeH->nX)+1, (g_pSnakeH->nY)+1);
	printf("o");
	gotoxy((g_nHeadX)+1, g_nHeadY+1);
	printf("O");


	Snake = CreateSnake(g_nHeadX,g_nHeadY);
	Snake->pNextNode = g_pSnakeH;
	g_pSnakeH = Snake;

	while(Snake->pNextNode != g_pSnakeT)
	{	
		Snake = Snake->pNextNode;
	}
	Snake->pNextNode=NULL;

	free(g_pSnakeT);
	g_pSnakeT = Snake;

	//state = CrashWall(g_nHeadX,g_nHeadY);

	return state;

}
void TailMove()
{

	g_nTailX = g_pSnakeH->nX;
	g_nTailY = g_pSnakeH->nY;

	switch(g_nHeadDir)
	{
	case EAST:
		g_nTailX++;
		break;
	case WEST: 
		g_nTailX--;
		break;
	case SOUTH:
		g_nTailY--;
		break;
	case NORTH:
		g_nTailY++;
		break;
	}

	gotoxy((g_pSnakeH->nX*2)+1, (g_pSnakeH->nY)+1);
	printf(" ");
	gotoxy((g_nTailX*2)+1, g_nTailY+1);
	printf("o");


	struct _Snake *Snake = CreateSnake(g_nTailX,g_nTailY);
	Snake->pNextNode = g_pSnakeH;
	g_pSnakeH=Snake;


}

void MapImage(){
	system("cls");
	for(int i=0;i<HEIGHT;i++)
	{	
		for(int j=0; j<WIDTH; j++)
		{
			switch(Map[i][j])
			{
			case 0:
				printf(" ");
				break;
			case 1:
				printf("@");
				break;
			default:
				break;
			}
		}printf("\n");
	}
}

int GameOverImage()
{	
	system("cls");
	gotoxy(17,10);
	printf("-----------------\n");
	gotoxy(17,11);
	printf("|   Game over   |\n");	
	gotoxy(17,12);
	printf("-----------------\n");

	return 0;
}
//create
struct _Snake* CreateSnake(int nX, int nY)
{
	struct _Snake *pNewNode=(struct _Snake*)malloc(sizeof(struct _Snake));

	if( pNewNode == NULL)
	{
		printf("CreateSnake() _ pNewNode = NULL\n");
		return NULL;
	} 

	pNewNode->nX = nX;
	pNewNode->nY = nY;
	pNewNode->pNextNode = NULL;

	return pNewNode;
}

